package com.carey.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carey.myblog.exception.NotFoundException;
import com.carey.myblog.pojo.Blog;
import com.carey.myblog.pojo.BlogTags;
import com.carey.myblog.pojo.Tag;
import com.carey.myblog.pojo.Type;
import com.carey.myblog.repository.TagRepository;
import com.carey.myblog.service.BlogService;
import com.carey.myblog.service.BlogTagsService;
import com.carey.myblog.service.TagService;
import com.carey.myblog.mapper.TagMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
* @author carey-
* @description 针对表【t_tag】的数据库操作Service实现
* @createDate 2022-05-25 22:09:57
*/
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>
    implements TagService{

    @Autowired
    private TagRepository repository;

    @Autowired
    private BlogService blogService;

    @Autowired
    private BlogTagsService blogTagsService;

    @Override
    public Integer saveTag(Tag tag) {
        return baseMapper.insert(tag);
    }

    @Override
    public Tag getTag(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Tag getTagByName(String name) {
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Page<Tag> listTag(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    public List<Tag> listTag() {
        return baseMapper.selectList(null);
    }

    @Override
    public List<Tag> listTagTop(Integer size) {
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.last("limit"+"\t"+size);
        //截取前size个元素
        //判断索引是否会越界
        List<Tag> tagList = baseMapper.selectList(wrapper);
        if (size>tagList.size()){
            return this.getBlogByTag(tagList);
        }
        return this.getBlogByTag(tagList).subList(0,size-1);
    }

    @Override
    public List<Tag> listTag(String ids) {
        return null;
    }

    @Override
    public Integer updateTag(Long id, Tag tag) {
        Tag tag1 = this.getTag(id);
        if (tag1 == null){
            throw new NotFoundException("该标签不存在");
        }
        else {
            BeanUtils.copyProperties(tag,tag1);
        }
        return baseMapper.updateById(tag1);
    }

    @Override
    public void deleteTag(Long id) {
        baseMapper.deleteById(id);
        QueryWrapper<BlogTags> wrapper = new QueryWrapper<>();
        wrapper.eq("tags_id",id);
        blogTagsService.remove(wrapper);
    }


    public List<Tag> getBlogByTag(List<Tag> list){
        list.forEach(tag ->{
            QueryWrapper<BlogTags> wrapper = new QueryWrapper<>();
            wrapper.eq("tags_id",tag.getId());
            List<BlogTags> blogList = blogTagsService.list(wrapper);
            tag.setBlogs(blogList);
        });
        return this.Descsort(list);
    }

    /**
     * 集合降序排序
     */
    public List<Tag> Descsort(List<Tag> list){
        list.sort(new Comparator<Tag>() {
            @Override
            public int compare(Tag o1, Tag o2) {
                return o2.getBlogs().size()-o1.getBlogs().size();
            }
        });
        return list;
    }
}




